from PIL import Image
import numpy as np


def extract_watermark_bmp(input_path, watermark_length_bits):
    img = Image.open(input_path)
    pixels = np.array(img)

    # 提取R通道LSB
    watermark_bin = ''
    extracted_bits = 0
    for i in range(pixels.shape[0]):
        for j in range(pixels.shape[1]):
            if extracted_bits < watermark_length_bits:
                watermark_bin += str(pixels[i, j, 0] & 1)
                extracted_bits += 1
            else:
                break

                # 二进制转字符串（UTF-8解码）
    watermark_bytes = []
    for i in range(0, watermark_length_bits, 8):
        byte = watermark_bin[i:i + 8]
        if len(byte) == 8:
            watermark_bytes.append(int(byte, 2))

    return bytes(watermark_bytes).decode('utf-8', errors='ignore')


# 示例：提取64位水印（对应8字节ASCII）
print(extract_watermark_bmp("bmpWaterMark/watermarked.bmp", 164))  # 输出：2025-03-30 20:00